//
//  ReadViewController.m
//  Leisure
//
//  Created by Alex on 16/6/28.
//  Copyright © 2016年 蓝鸥科技有限公司西安分公司. All rights reserved.
//

#import "ReadViewController.h"

@interface ReadViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic,strong)UICollectionView *collectionView;
// 放请求下来的模型
@property (nonatomic,strong)NSMutableArray *dataSouce;
// 放轮播图
@property (nonatomic,strong)NSMutableArray *scrollArray;
@end

@implementation ReadViewController

- (UICollectionView *)collectionView {
    
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        // 设置item大小
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, kHeight/3+64, kWidth, kHeight/2+64) collectionViewLayout:layout];
        layout.itemSize = CGSizeMake(kWidth/3-10, kWidth/3-5);
        layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
        
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
    }
    return _collectionView;
}

- (NSMutableArray *)dataSouce {
    
    if (!_dataSouce) {
        _dataSouce = [NSMutableArray array];
    }
    return _dataSouce;
}

- (NSMutableArray *)scrollArray {
    
    if (!_scrollArray) {
        _scrollArray = [NSMutableArray array];
    }
    return _scrollArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 参数没有时请写 parameter:@{} 不写nil,因为不好看
    [RequestManager fecthDataWithUrl:kReadUrl parameter:@{} mehtod:@"GET" compelet:^(NSData *data) {
        
        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        
        // 添加轮播图的数据源
        for (NSDictionary *smallDic in bigDic[@"data"][@"carousel"]) {
            
            [self.scrollArray addObject:smallDic[@"img"]];
        }
        // 添加collectionView的数据源
        for (NSDictionary *smallDic in bigDic[@"data"][@"list"]) {
            
            ReadModel *model = [[ReadModel alloc]init];
            [model setValuesForKeysWithDictionary:smallDic];
            [self.dataSouce addObject:model];
        }
        
        
    } updateUI:^{
        
        // 创建轮播图 (要放在刷新UI，放在block外面数组是没有值的)
        ZHScrollView *scroll = [[ZHScrollView alloc]initWithFrame:CGRectMake(0, 64, kWidth, kHeight/3) imageArray:self.scrollArray];
        [self.view addSubview:scroll];
       
        [self.collectionView reloadData];
    }];
    
    // 注册XIB (默认里面只有一个XIB，出现两个就蹦)
    [self.collectionView registerNib:[UINib nibWithNibName:@"ReadCell" bundle:nil] forCellWithReuseIdentifier:@"read_cell"];
    
    [self.view addSubview:self.collectionView];
//    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"read_cell"];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
}

#pragma mark ----------collectionViewDelegate-----------

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.dataSouce.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ReadCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"read_cell" forIndexPath:indexPath];
    
    [cell setCellWithModel:self.dataSouce[indexPath.row]];
    
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    // 通过xib来获取控制器 第一种方法
    ReadListViewController *readListVC = [[ReadListViewController alloc]initWithNibName:@"ReadListViewController" bundle:nil];
    // 只能用来获取View，不能获取控制器
//    ReadListViewController *readListVC = [[NSBundle mainBundle]loadNibNamed:@"ReadListViewController" owner:nil options:nil].firstObject;
    ReadModel *model = self.dataSouce[indexPath.row];
    readListVC.typeID = model.type;
    
    [self.navigationController pushViewController:readListVC animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
 
}



@end
